
package com.fruit.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * CommonUtil
 * 
 * @author Administrator
 * @version 2017.12.01 11:40
 */
public class CommonUtil {

	/**
	 * 返回MD5编码
	 * @return 返回MD5编码
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getNonceStr() {
		Random random = new Random();
		return MD5Util.MD5Encode(String.valueOf(random.nextInt(10000)), "UTF-8");
	}

	/**
	 * 返回时间戳
	 * 
	 * @return 返回时间戳
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getTimeStamp() {
		return String.valueOf(System.currentTimeMillis() / 1000);
	}

	/**
	 * map转xml
	 * 
	 * @param vo
	 *            vo
	 * @param rootElement
	 *            rootElement
	 * @return 返回xml
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String map2xmlBody(Map<String, String> vo, String rootElement) {
		Element body = DocumentHelper.createElement(rootElement);
		buildMap2xmlBody(body, vo);
		return body.asXML();
	}

	private static void buildMap2xmlBody(Element body, Map<String, String> vo) {
		if (vo != null) {
			Iterator<String> it = vo.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				if (StringUtils.isNotEmpty(key)) {
					Element element = DocumentHelper.createElement(key);
					element.add(DocumentHelper.createCDATA(vo.get(key)));

					body.add(element);
				}
			}
		}
	}

	/**
	 * xml转map
	 * 
	 * @param xml
	 *            xml
	 * @param rootElement
	 *            rootElement
	 * @return 返回map
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static Map<String, String> xmlBody2map(String xml, String rootElement) {
		Document doc;
		Map<String, String> vo = null;
		try {
			doc = DocumentHelper.parseText(xml);
			Element body = (Element) doc.selectSingleNode(rootElement);
			vo = buildXmlBody2map(body);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return vo;
	}

	private static Map<String, String> buildXmlBody2map(Element body) {
		Map<String, String> vo = new TreeMap<String, String>();
		if (body != null) {
			List<Element> elements = body.elements();
			for (Element element : elements) {
				String key = element.getName();
				if (StringUtils.isNotEmpty(key)) {
					String text = element.getText().trim();
					vo.put(key, text);
				}
			}
		}
		return vo;
	}

	/**
	 * utf-8转码
	 * 
	 * @param str
	 *            str
	 * @param en
	 *            en
	 * @return utf-8转码
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String encode(String str, String en) {
		if (StringUtils.isNotEmpty(str)) {
			try {
				return URLEncoder.encode(str, en);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return "";
			}
		} else {
			return "";
		}
	}

	/**
	 * utf-8转码
	 * 
	 * @param str
	 *            str
	 * @param en
	 *            en
	 * @return utf-8转码
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String decode(String str, String en) {
		if (StringUtils.isNotEmpty(str)) {
			try {
				return URLDecoder.decode(str, en);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "";
			}
		} else {
			return "";
		}

	}

	/**
	 * makeDisInfo
	 * 
	 * @param min
	 *            min
	 * @param max
	 *            max
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static void makeDisInfo(double min, double max) {
		BigDecimal minB = new BigDecimal(min).setScale(2, BigDecimal.ROUND_HALF_DOWN);
		BigDecimal maxB = new BigDecimal(max).setScale(2, BigDecimal.ROUND_HALF_DOWN);
		BigDecimal temp = minB;
		BigDecimal max8 = new BigDecimal("8");

		BigDecimal pointsRatio = new BigDecimal("100");
		BigDecimal points2 = new BigDecimal("2");
		BigDecimal points6 = new BigDecimal("6");
		BigDecimal points7 = new BigDecimal("7");
		BigDecimal points8 = new BigDecimal("8");
		BigDecimal points85 = new BigDecimal("8.5");
		BigDecimal points9 = new BigDecimal("9");

		BigDecimal offlineRatio = new BigDecimal("100");
		BigDecimal offline8 = new BigDecimal("8");
		BigDecimal offline83 = new BigDecimal("8.3");
		BigDecimal offline85 = new BigDecimal("8.5");
		BigDecimal offline88 = new BigDecimal("8.8");
		BigDecimal offline9 = new BigDecimal("9.0");
		BigDecimal offline95 = new BigDecimal("9.5");
		while (temp.compareTo(maxB) <= 0) {

			BigDecimal pRatio = new BigDecimal("0.1");
			BigDecimal cRatio = new BigDecimal("1");
			if (temp.compareTo(max8) < 0) {
				cRatio = max8.subtract(temp).multiply(new BigDecimal("5")).setScale(0, BigDecimal.ROUND_DOWN)
						.add(new BigDecimal("1"));
			}

			if (temp.compareTo(points2) >= 0 && temp.compareTo(points6) <= 0) {
				pointsRatio = new BigDecimal("100");
			} else if (temp.compareTo(points6) > 0 && temp.compareTo(points7) <= 0) {
				pointsRatio = new BigDecimal("50");
			} else if (temp.compareTo(points7) > 0 && temp.compareTo(points8) <= 0) {
				pointsRatio = new BigDecimal("30");
			} else if (temp.compareTo(points8) > 0 && temp.compareTo(points85) <= 0) {
				pointsRatio = new BigDecimal("10");
			} else if (temp.compareTo(points85) > 0 && temp.compareTo(points9) <= 0) {
				pointsRatio = new BigDecimal("5");
			} else {
				pointsRatio = new BigDecimal("0");
			}

			if (temp.compareTo(offline8) <= 0) {
				offlineRatio = new BigDecimal("100");
			} else if (temp.compareTo(offline8) > 0 && temp.compareTo(offline83) <= 0) {
				offlineRatio = new BigDecimal("80");
			} else if (temp.compareTo(offline83) > 0 && temp.compareTo(offline85) <= 0) {
				offlineRatio = new BigDecimal("65");
			} else if (temp.compareTo(offline85) > 0 && temp.compareTo(offline88) <= 0) {
				offlineRatio = new BigDecimal("50");
			} else if (temp.compareTo(offline88) > 0 && temp.compareTo(offline9) <= 0) {
				offlineRatio = new BigDecimal("35");
			} else if (temp.compareTo(offline9) > 0 && temp.compareTo(offline95) <= 0) {
				offlineRatio = new BigDecimal("10");
			}

			StringBuffer sb = new StringBuffer();
			sb.append("insert into hltx_bonus_config(discount,p_ratio,c_ratio,points_ratio,offline_ratio)")
					.append("values(").append(temp).append(",").append(pRatio).append(",").append(cRatio).append(",")
					.append(pointsRatio).append(",").append(offlineRatio).append(");");

			System.out.println(sb.toString());

			temp = temp.add(new BigDecimal("0.01"));
		}

	}

	/**
	 * 获取本地ip
	 * 
	 * @param isLinux
	 *            isLinux
	 * @return 返回ip
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getLocalIP(boolean isLinux) {
		String ip = "";
		try {
			if (isLinux) {
				Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
				while (e1.hasMoreElements()) {
					NetworkInterface ni = (NetworkInterface) e1.nextElement();
					if (!ni.getName().equals("eth0")) {
						continue;
					} else {
						Enumeration<?> e2 = ni.getInetAddresses();
						while (e2.hasMoreElements()) {
							InetAddress ia = (InetAddress) e2.nextElement();
							if (ia instanceof Inet6Address) {
								continue;
							}
							ip = ia.getHostAddress();
						}
						break;
					}
				}
			} else {
				ip = InetAddress.getLocalHost().getHostAddress().toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return ip;
	}


	/**
	 * 获取UUID
	 * @return 返回UUID
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		return uuid;
	}
}